package img 
{
	import flash.display.*;
	import flash.geom.*;
	import flash.text.*;
	import ru.gosoplata.model.*;
	
	/**
	 * ...
	 * @author a.orleansky
	 */
	public class InputPlane extends Sprite 
	{
		private var in_left:Bitmap=new Bitmap();
		private var in_right:Bitmap=new Bitmap();
		private var in_center:Bitmap=new Bitmap();
		private var inch_left:Bitmap=new Bitmap();
		private var inch_right:Bitmap=new Bitmap();
		private var inch_center:Bitmap=new Bitmap();
		private var _width:Number;

		private var input_txt:TextField;
		private var _mask:Number = 0; //0-no mask; 1-phone; 2-card

		public function InputPlane(width:Number) 
		{
			_width = width;
			createInpuPlane();
		}
		private var img1:Bitmap;
		private var img2:Bitmap;
		private function createImg1():void {
			img1.bitmapData = new BitmapData(in_left.width + in_center.width + in_right.width, in_left.height);
			img1.bitmapData.copyPixels(in_left.bitmapData, new Rectangle(0, 0, in_left.width, in_left.height), new Point(0, 0));
			var fullwd:Number = in_center.width;
			var wd:Number = in_center.bitmapData.width;
			for (var ii:Number = 0; ii < Math.floor(fullwd / wd);ii++){
				img1.bitmapData.copyPixels(in_center.bitmapData, new Rectangle(0, 0, wd, in_center.height), new Point(in_left.width+wd*ii, 0));
			}
			img1.bitmapData.copyPixels(in_center.bitmapData, new Rectangle(0, 0, fullwd-wd*(ii-1), in_center.height), new Point(in_left.width+wd*ii, 0));
			img1.bitmapData.copyPixels(in_right.bitmapData, new Rectangle(0, 0, in_right.width, in_right.height), new Point(in_left.width + in_center.width, 0));
			img1.smoothing = true;
		}
		private function createImg2():void {
			img2.bitmapData = new BitmapData(inch_left.width + inch_center.width + inch_right.width, inch_left.height);
			img2.bitmapData.copyPixels(inch_left.bitmapData, new Rectangle(0, 0, inch_left.width, inch_left.height), new Point(0, 0));
			var fullwd:Number = inch_center.width;
			var wd:Number = inch_center.bitmapData.width;
			for (var ii:Number = 0; ii < Math.floor(fullwd / wd);ii++){
				img2.bitmapData.copyPixels(inch_center.bitmapData, new Rectangle(0, 0, wd, inch_center.height), new Point(inch_left.width+wd*ii, 0));
			}
			img2.bitmapData.copyPixels(inch_center.bitmapData, new Rectangle(0, 0, fullwd-wd*(ii-1), inch_center.height), new Point(inch_left.width+wd*ii, 0));
			img2.bitmapData.copyPixels(inch_right.bitmapData, new Rectangle(0, 0, inch_right.width, inch_right.height), new Point(inch_left.width + inch_center.width, 0));
			img2.smoothing = true;
		}
		private function createInpuPlane():void 
		{
			in_left.bitmapData = TmpModel.loadedImages.getBitmapDataByName("input_plane_left").bitmapData;
			in_right.bitmapData = TmpModel.loadedImages.getBitmapDataByName("input_plane_right").bitmapData;
			in_center.bitmapData = TmpModel.loadedImages.getBitmapDataByName("input_plane_center").bitmapData;
			inch_left.bitmapData = TmpModel.loadedImages.getBitmapDataByName("input_plane_chek_left").bitmapData;
			inch_right.bitmapData = TmpModel.loadedImages.getBitmapDataByName("input_plane_chek_right").bitmapData;
			inch_center.bitmapData = TmpModel.loadedImages.getBitmapDataByName("input_plane_chek_center").bitmapData;
			in_center.width = _width - in_left.width - in_right.width;
			inch_center.width = in_center.width;
			
			//in_left.x = 0;
			//in_center.x = in_left.x + in_left.width-7;
			//in_right.x = in_center.x + in_center.width-7;
			
			img1 = new Bitmap();
			img1.x = 0;
			img1.y = 0;
			addChild(img1);
			createImg1();
			img2 = new Bitmap();
			img2.x = 0;
			img2.y = 0;
			addChild(img2);
			createImg2();
			
			var txt_type:TextFormat = TmpModel.getTextFormat(52, 0x676767, true);
			txt_type.letterSpacing = 7;

			input_txt = new TextField();
			input_txt.embedFonts = true;
			input_txt.defaultTextFormat = txt_type;
			input_txt.y = in_center.y + in_center.height / 2 - input_txt.height / 3;
			input_txt.x = in_left.width-7;
			input_txt.width = in_center.width;
			input_txt.height = 60;
			input_txt.thickness = 100;
			input_txt.selectable = false;
			input_txt.antiAliasType = AntiAliasType.ADVANCED;
			input_txt.wordWrap = true;
			input_txt.multiline = true;
			
			addChild(input_txt);
			
		}
		
		public function set text(str:String):void {
			switch(_mask) {
				case 0:
					input_txt.text = str;
					break;
				case 1://phone
					input_txt.text = preparePhoneTXT(str);
					break;
				case 2://card
					input_txt.text = prepareCardTXT(str);
					break;
				case 3://mes
					input_txt.text = prepareMesTXT(str);
					break;
				case 4://nic
					input_txt.text = prepareNicTXT(str);
					break;
				case 5://io
					input_txt.text = prepareIoTXT(str);
					break;
				case 6://date
					input_txt.text = prepareDateTXT(str);
					break;
				case 7://stream
					input_txt.text = prepareStreamTXT(str);
					break;
				case 8://password
					input_txt.text = preparePassTXT(str);
					break;
			}
			input_txt.scrollV = input_txt.maxScrollV;
		}
		
		private function preparePassTXT(str:String):String
		{
			var val:String = "****************";
			val = val.substr(0, str.length);
			return val;
		}
		
		private function prepareStreamTXT(str:String):String 
		{
			var card:String = str.concat("        ");
			var rcard:String = "";
			card = card.substr(0,8);
			rcard = card.substr(0, 6) + "-" + card.substr(6);
			return rcard;
			
		}
		
		private function prepareDateTXT(str:String):String 
		{
			var card:String = str.concat("      ");
			var rcard:String = "";
			card = card.substr(0,6);
			rcard = card.substr(0,2)+"/"+card.substr(2,2)+"/20"+card.substr(4);
			return rcard;
		}
		
		private function prepareIoTXT(str:String):String 
		{
			var card:String = str.concat("  ");
			var rcard:String = "";
			card = card.substr(0,2);
			rcard = card.charAt(0)+"/"+card.charAt(1);
			return rcard;
		}
		
		private function prepareNicTXT(str:String):String 
		{
			var card:String = "      "+str;
			var rcard:String = "";
			card = card.substr((card.indexOf("/")-6));
			rcard = card;
			return rcard;
			
		}
		
		public function getText():String {
			return input_txt.text;
		}
		
		private function prepareCardTXT(str:String):String { 
			var card:String = str.concat("                ");
			var rcard:String = "";
			card = card.substr(0, 16);
			rcard = card.substr(0, 4) + "-" + card.substr(4, 4) + "-"+card.substr(8, 4) + "-"+card.substr(12, 4);
			return rcard;
		}
		
		private function preparePhoneTXT(str:String):String { 
			var card:String = str.concat("          ");
			var rcard:String = "";
			card = card.substr(0, 10);
			rcard = "8("+card.substr(0, 3) + ")" + card.substr(3, 3) + "-"+card.substr(6, 2) + "-"+card.substr(8);
			return rcard;
		}
		
		private function prepareMesTXT(str:String):String {
			var card:String = str.concat("          ");
			var rcard:String = "";
			card = card.substr(0, 10);
			rcard = card.substr(0, 5) + "-" + card.substr(5, 3) + "-" + card.substr(8);
			return rcard;
		}
		
		public function set txtmask(value:Number):void 
		{
			_mask = value;
		}
		
		public function get txtmask():Number {
			return _mask;
		}
		public function reScale(wd:Number):void {
			in_center.width = wd - in_left.width - in_right.width;
			inch_center.width = in_center.width;
			
			//in_left.x = 0;
			//in_center.x = in_left.x + in_left.width-7;
			//in_right.x = in_center.x + in_center.width-7;
			createImg1();
			createImg2();
			input_txt.x = in_left.width-7;
			input_txt.width = in_center.width;			
			input_txt.restrict = null;
			cacheAsBitmap = true;
		}
		
		public function setRestrickt(str:String):void {
			input_txt.restrict = str;
		}
		
		public function isCheck(sw:Boolean):void {
			img1.visible = !sw;
			img2.visible = sw;
		}
	}

}